<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bit Operations</title>
<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../egs.html" title="Examples">
<link rel="prev" href="factorials.html" title="Factorials">
<link rel="next" href="../../floats.html" title="Floating-point Types">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="factorials.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../egs.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../floats.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="boost_multiprecision.tut.ints.egs.bitops"></a><a class="link" href="bitops.html" title="Bit Operations">Bit Operations</a>
</h5></div></div></div>
<p>
            In this example we'll show how individual bits within an integer may
            be manipulated, we'll start with an often needed calculation of <span class="emphasis"><em>2<sup>n</sup> -
            1</em></span>, which we could obviously implement like this:
          </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_int</span><span class="special">;</span>

<span class="identifier">cpp_int</span> <span class="identifier">b1</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">cpp_int</span> <span class="identifier">r</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
   <span class="keyword">return</span> <span class="special">(</span><span class="identifier">r</span> <span class="special">&lt;&lt;</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Calling:
          </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showbase</span> <span class="special">&lt;&lt;</span> <span class="identifier">b1</span><span class="special">(</span><span class="number">200</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
            Yields as expected:
          </p>
<pre class="programlisting">0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF</pre>
<p>
            However, we could equally just set the n'th bit in the result, like this:
          </p>
<pre class="programlisting"><span class="identifier">cpp_int</span> <span class="identifier">b2</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">cpp_int</span> <span class="identifier">r</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
   <span class="keyword">return</span> <span class="special">--</span><span class="identifier">bit_set</span><span class="special">(</span><span class="identifier">r</span><span class="special">,</span> <span class="identifier">n</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
            Note how the <code class="computeroutput"><span class="identifier">bit_set</span></code>
            function sets the specified bit in its argument and then returns a reference
            to the result - which we can then simply decrement. The result from a
            call to <code class="computeroutput"><span class="identifier">b2</span></code> is the same
            as that to <code class="computeroutput"><span class="identifier">b1</span></code>.
          </p>
<p>
            We can equally test bits, so for example the n'th bit of the result returned
            from <code class="computeroutput"><span class="identifier">b2</span></code> shouldn't be
            set unless we increment it first:
          </p>
<pre class="programlisting"><span class="identifier">BOOST_MP_ASSERT</span><span class="special">(!</span><span class="identifier">bit_test</span><span class="special">(</span><span class="identifier">b1</span><span class="special">(</span><span class="number">200</span><span class="special">),</span> <span class="number">200</span><span class="special">));</span>     <span class="comment">// OK</span>
<span class="identifier">BOOST_MP_ASSERT</span><span class="special">(</span><span class="identifier">bit_test</span><span class="special">(++</span><span class="identifier">b1</span><span class="special">(</span><span class="number">200</span><span class="special">),</span> <span class="number">200</span><span class="special">));</span>    <span class="comment">// OK</span>
</pre>
<p>
            And of course if we flip the n'th bit after increment, then we should
            get back to zero:
          </p>
<pre class="programlisting"><span class="identifier">BOOST_MP_ASSERT</span><span class="special">(!</span><span class="identifier">bit_flip</span><span class="special">(++</span><span class="identifier">b1</span><span class="special">(</span><span class="number">200</span><span class="special">),</span> <span class="number">200</span><span class="special">));</span>   <span class="comment">// OK</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="factorials.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../egs.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../floats.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
